//
//  HXMessageDetailsTableViewController.m
//  KD
//
//  Created by ifly on 16/3/24.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXMessageDetailsTableViewController.h"
#import "HXItemBaseMessageTableViewCell.h"
#import "HXItemCorpMessageTableViewCell.h"
#import "HXItemFenbaoMessageTableViewCell.h"
#import "HXRightButtonView.h"
#import "HXDropdownMenu.h"
#import "HXChangeViewController.h"
#import "HXMessageDetailsModel.h"

@interface HXMessageDetailsTableViewController ()
@property(nonatomic,strong) NSArray *baseMsgArray;
@property(nonatomic,strong) NSArray *corpArray;
@property(nonatomic,strong) NSArray *fenBaoArray;
@property(nonatomic,copy)NSString *keyGuid;
//@property(nonatomic,assign)BOOL isHide;
//@property(nonatomic,weak) HXDropdownMenu *menu;
//@property(nonatomic,strong)HXRightButtonView * hxgisView;
//@property (strong, nonatomic) UIView *maskView;//背景View

@property (nonatomic,retain) HXMessageDetailsModel * messageDetailsModel;

@end

@implementation HXMessageDetailsTableViewController
+(instancetype)initWithkeyGuid:(NSString *)keyGuid{
    HXMessageDetailsTableViewController *vc = [[HXMessageDetailsTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    vc.keyGuid = keyGuid;
    return vc;
}

- (void)viewDidLoad {
//    _isHide = YES;
    [super viewDidLoad];
    self.title = @"工地现场信息";
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 200;
    [self.tableView registerNib:[UINib nibWithNibName:@"HXItemBaseMessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"HXItemBaseMessageTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HXItemCorpMessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"HXItemCorpMessageTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HXItemFenbaoMessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"HXItemFenbaoMessageTableViewCell"];
    self.tableView.backgroundColor = HXCustomColor(222, 222, 222, 1);
    
//    HXChangeViewController * hxChangeVC = [[HXChangeViewController alloc] init];
//    hxChangeVC.sendKeyGuid = _keyGuid;
//    
    //[SVProgressHUD showWithStatus:@"正在获取" maskType:SVProgressHUDMaskTypeBlack];

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightBarButtonClick:)];
//    _hxgisView = [[[NSBundle mainBundle] loadNibNamed:@"HXRightButtonView" owner:nil options:nil] lastObject];
//    _hxgisView.frame = CGRectMake(self.view.frame.size.width - 120 - 20, 0, 120, 32);
//    _hxgisView.hidden = YES;
//    [self.view addSubview:_hxgisView];
//    _hxgisView.KeyGuid = _keyGuid;
    
    [SVProgressHUD showWithStatus:@"正在获取"];

    [NetworkAPI getItemDetailsWithKeyGuid:_keyGuid
                                  success:^(HXMessageDetailsModel *result) {
                                      [SVProgressHUD dismiss];
                                      if ([result.Code isEqualToString:@"200"]) {
                                          _messageDetailsModel = result;
                                          self.baseMsgArray = result.messageArray;
                                          self.corpArray = result.Corplist;
                                          self.fenBaoArray = result.SingleProjectContract;
                                          
//                                          _hxgisView.PrjName = result.Projectinfo.PrjName;
//                                          _hxgisView.BuildCorpName = result.Projectinfo.BuildCorpName;
//                                          NSLog(@"%@  %@",hxChangeVC.sendPrjName,hxChangeVC.sendBuildCorpName);
                                          
                                          [self.tableView reloadData];
                                      }else{
                                          [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                      }
                                  } failure:^(NSError *err) {
                                      [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                  }];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更新项目状态" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonBtnAct)];
}

/**更改项目状态按钮触发事件*/
-(void)rightBarButtonBtnAct{
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"更新项目状态:竣工" message:[NSString stringWithFormat:@"确定要把 “ %@ ” 项目设置为竣工吗？",_messageDetailsModel.Projectinfo.PrjName] preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [NetworkAPI changeRedProgectStateWithKeyGuid:_keyGuid
                                              UserID:[UserDefaultHandler getUserDetault:kID]
                                             Success:^(HXBaseModel *result) {
                                                 if ([result.code isEqualToString:@"200"]) {
                                                     [SVProgressHUD showSuccessWithStatus:@"更改成功"];
                                                 }
                                             }
                                             failure:^(NSError *err) {
                                                 [SVProgressHUD showErrorWithStatus:@"更改失败"];
                                             }];
    }]];

}

//-(void)rightBarButtonClick:(UIBarButtonItem *)btn {
//    
//    if (_isHide == YES) {
//        _hxgisView.hidden = NO;
//        _isHide = NO;
//        _maskView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//        _maskView.backgroundColor=[UIColor blackColor];
//        _maskView.alpha=0.4;
//        [self.view addSubview:_maskView];
//        [self.view bringSubviewToFront:_hxgisView];
//        return;
//    }
//    if (_isHide == NO) {
//        _hxgisView.hidden = YES;
//        _isHide = YES;
//        [self.maskView removeFromSuperview];
//        return;
//    }
//}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    
//    _hxgisView.hidden = YES;
//    _isHide = YES;
//    [self.maskView removeFromSuperview];
//    
//    NSLog(@"点击事件的层次");
//    
//}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
       return  self.baseMsgArray.count;
    }else if (section == 1){
       return  self.corpArray.count;
    }else if (section == 2){
        return  self.fenBaoArray.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HXItemBaseMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXItemBaseMessageTableViewCell" forIndexPath:indexPath];
        cell.data = self.baseMsgArray[indexPath.row];
        if (indexPath.row%2 == 0)cell.backgroundColor = [UIColor whiteColor];
        if (indexPath.row%2 != 0)cell.backgroundColor = HXCustomColor(239, 239, 244, 1);
        return cell;
    }else if (indexPath.section == 1){
        HXItemCorpMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXItemCorpMessageTableViewCell" forIndexPath:indexPath];
        cell.data = self.corpArray[indexPath.row];
        cell.sendModelStr = _messageDetailsModel.Projectinfo.PrjNum;
        return cell;
    }else if (indexPath.section == 2){//分包
        HXItemFenbaoMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXItemFenbaoMessageTableViewCell" forIndexPath:indexPath];
        cell.data = self.fenBaoArray[indexPath.row];
        return cell;
    }else{
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 49;
    }
    else if (indexPath.section == 1) {
        return 172;
    }
    else if (indexPath.section == 2) {
        
        return 65;
    }
    return 0;
}



-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"项目基本信息";
    }else if (section == 1){
        return @"参建单位基本信息";
    }else if (section == 2){
        if (self.fenBaoArray.count == 0) {
            return @"分包单位基本信息:  暂无";
        }else{
            return @"分包单位基本信息";
        }
    }else{
        return @"";
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - TableView分割线左对齐
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
@end
